package concurrence;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author wxh
 */
public class Kettle {

	public static void main(String[] args) throws ExecutionException, InterruptedException {

		// 创建任务FutureTask2
		FutureTask<String> futureTask2 = new FutureTask(new Task2());

		// 创建任务FutureTask1，并把任务T1的FutureTask传进去
		FutureTask<String> futureTask1 = new FutureTask(new Task1(futureTask2));

		// 线程1执行任务任务FutureTask1
		Thread thread1 = new Thread(futureTask1);

		// 线程2执行任务任务FutureTask2
		Thread thread2 = new Thread(futureTask2);

		thread1.start();
		thread2.start();

		// 阻塞等待线程T1执行futureTask1的结果
		System.out.println(futureTask1.get());

	}

	static class Task1 implements Callable<String> {

		private FutureTask<String> futureTask2;
		public Task1(FutureTask<String> futureTask2) {
			this.futureTask2 = futureTask2;
		}

		@Override
		public String call() throws Exception {

			System.out.println("T1:洗【水】壶...");
			TimeUnit.SECONDS.sleep(1);

			System.out.println("T1:正在烧开水，需要15秒...");
			TimeUnit.SECONDS.sleep(15);

			String tea = futureTask2.get();

			System.out.println("T1:拿到茶叶:"+tea);

			System.out.println("T1:泡茶...");
			return "上茶:" + tea;

		}
	}

	static class Task2 implements Callable<String> {

		@Override
		public String call() throws Exception {
			System.out.println("T2:洗【茶】壶...");
			TimeUnit.SECONDS.sleep(1);

			System.out.println("T2:洗茶杯...");
			TimeUnit.SECONDS.sleep(1);

			System.out.println("T2:拿茶叶...");
			TimeUnit.SECONDS.sleep(1);
			return "龙井";
		}
	}

}